//
//  TSGameTableViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/12/11.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

extension NSNotification.Name {
    public static let TSRefreshCountDownModelNotification = Notification.Name("TSRefreshCountDownModelNotification")
}

protocol TSGameTableViewCellDelegate : NSObjectProtocol {
    func didClickCell(model:TSMainPageModel)
}


//MARK: - TSGameCollectionCell
class TSGameCollectionCell: UICollectionViewCell {
    
    var img : UIImageView!
    var titleLabel : UILabel!
    var model : TSMainPageModel? = nil
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configViews()

    }
    
    func setupCell(model:TSMainPageModel) {
        
        self.model = model
        img.image = UIImage.init(named: model.imageName)
        titleLabel.text = model.title
    }
    
    required init?(coder: NSCoder) {
        
        super.init(coder: coder)
        
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension TSGameCollectionCell {
    
    func configViews() {
        
        img = UIImageView.init()
        contentView.addSubview(img)
        img.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }

        titleLabel = UILabel.init()
        titleLabel.textColor = WHITE_COLOR
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-6)
        }
    }
}

//MARK: - TSGameTableViewCell
class TSGameTableViewCell: UITableViewCell {
    
    weak var delegate : TSGameTableViewCellDelegate? = nil
//    var model : TSMainPageModel? = nil
    var collectionView : UICollectionView!
    var dataSource : Array<TSMainPageModel> = []

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        configViews()
        
    }
    
    func setupCell(array:Array<TSMainPageModel>) {
        self.dataSource = array
        collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension TSGameTableViewCell : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(TSGameCollectionCell.self), for: indexPath) as? TSGameCollectionCell
                
        cell?.setupCell(model: dataSource[indexPath.item])
        
        return cell!
    }
}

extension TSGameTableViewCell : UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if self.delegate != nil {
            self.delegate?.didClickCell(model: self.dataSource[indexPath.item])
        }
    }
}

extension TSGameTableViewCell {
    
    func configViews() {
        
        let icon = UIImageView.init(image: UIImage(named: "main_game"))
        contentView.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(15)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = "热门游戏"
        titleLabel.font = UIFont.systemFont(ofSize: 15)
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#454545")
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(icon)
            make.left.equalTo(icon.snp.right).offset(5)
        }
        
        let imgScale : CGFloat = 167.0 / 130
        let collectionW = CGFloat(SCREEN_WIDTH - 2*15)
        let itemSpacing : CGFloat = 10
        let itemW = (collectionW - itemSpacing) * 0.5
        let itemH = itemW / imgScale

        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: itemW, height: itemH)
        layout.minimumInteritemSpacing = itemSpacing

        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = WHITE_COLOR
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.bounces = false
        collectionView.isScrollEnabled = false
        collectionView.register(TSGameCollectionCell.self, forCellWithReuseIdentifier: NSStringFromClass(TSGameCollectionCell.self))
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalTo(icon.snp.bottom).offset(15)
            make.size.equalTo(CGSize(width: collectionW, height: itemH))
        }
    }
}
